
//
//  Created by henry on 13-3-25.
//  Copyright (c) 2013年 henry. All rights reserved.
//

#import "MemberViewController.h"
#import "Utility.h"
#import "QuartzCore/QuartzCore.h"
#import "Config.h"
#import "MemberViewCell.h"
#import "HomePageViewController.h"
#import "AppDelegate.h"
#import "BowenMemberViewController.h"

@interface MemberViewController(private)

-(void) onButtonClick:(UIButton*) sender;
@end



@implementation MemberViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
    self.navigationItem.titleView=[Utility getTitleView:@"成员"];
    //导航栏左边按钮
    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(onClickAction:) normalImage:@"pushButton_normal.png" lightedImage:@"pushButton_hover.png" frame:CGRectMake(0, 0, 35, 35) title:nil tag:PUSHMENUBUTTONTAG]];
    //导航栏右边按钮
    self.navigationItem.rightBarButtonItem=[[UIBarButtonItem alloc] initWithCustomView:[Utility customButton:self action:@selector(onClickAction:) normalImage:@"moreButton_normal.png" lightedImage:@"moreButton_hover.png" frame:CGRectMake(0, 0, 35, 35) title:nil tag:PUSHMENUBUTTONTAG+1]];
    //*UIsearchbar
    self.customSearchBar = [[CustomSearchBar alloc] initWithFrame:CGRectMake(0.0f, 10, self.view.bounds.size.width, 39.0f)];
    [self.customSearchBar setBarBackgroundImage:[UIImage imageNamed:@"search_background.png"]];
    self.customSearchBar.delegate = self;
    [self.customSearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
    [self.customSearchBar sizeToFit];
    [self.view addSubview:self.customSearchBar];
    
    //供稿数 收藏数 评论数 头像
    UIView *viewtop=[[UIView alloc] initWithFrame:CGRectMake(0, 60, 320, 60)];
    viewtop.backgroundColor=[UIColor colorWithPatternImage:[Utility imageNamed:@"member_top_bg.png"]];
    UIImageView *logo=[[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 40, 40)];
    NSString *logostr=@"";
    if (logostr&&logostr.length>0) {
        logo.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@",[Utility getServerUrl],logostr]]]];
    }else{
        logo.image=[Utility imageNamed:@"cg_logo.png"];
    }
    [logo.layer setMasksToBounds:YES];
    [logo.layer setCornerRadius:20.0f];
    [viewtop addSubview:logo];
    UILabel *quanziname=[Utility detailTextLabel:CGRectMake(54, 24, 100, 20) andFontSize:12.0f];
    quanziname.textColor=[UIColor blackColor];
    quanziname.text=@"1798履行者";
    [viewtop addSubview:quanziname];
    
    UIButton *button1=[Utility customButton:self action:@selector(onButtonClick:) normalImage:@"memeber_gonggao.png" lightedImage:@"memeber_gonggao.png" frame:CGRectMake(145, 10, 49, 45) title:nil tag:MEMBERHOMEBUTTONTAG];
    UILabel *label1=[Utility detailTextLabel:CGRectMake(0, 22, 49, 15) andFontSize:12.0];
    label1.textAlignment=NSTextAlignmentCenter;
    label1.textColor=[UIColor blackColor];
    label1.text=@"233";
    [button1 addSubview:label1];
    [viewtop addSubview:button1];
    
    UIButton *button2=[Utility customButton:self action:@selector(onButtonClick:) normalImage:@"memeber_pinglun.png" lightedImage:@"memeber_pinglun.png" frame:CGRectMake(260, 10, 49, 45) title:nil tag:MEMBERHOMEBUTTONTAG+1];
    UILabel *label2=[Utility detailTextLabel:CGRectMake(0, 22, 49, 15) andFontSize:12.0];
    label2.textAlignment=NSTextAlignmentCenter;
    label2.textColor=[UIColor blackColor];
    label2.text=@"233";
    [button2 addSubview:label2];
    [viewtop addSubview:button2];
    
    UIButton *button3=[Utility customButton:self action:@selector(onButtonClick:) normalImage:@"memeber_shoucang.png" lightedImage:@"memeber_shoucang.png" frame:CGRectMake(202, 10, 49, 45) title:nil tag:MEMBERHOMEBUTTONTAG+2];
    UILabel *label3=[Utility detailTextLabel:CGRectMake(0, 22, 49, 15) andFontSize:12.0];
    label3.textAlignment=NSTextAlignmentCenter;
    label3.textColor=[UIColor blackColor];
    label3.text=@"233";
    [button3 addSubview:label3];
    [viewtop addSubview:button3];
    [self.view addSubview:viewtop];
	// Do any additional setup after loading the view.
    
    
    float tableHeight=0.0f;
    if (!iPhone5) {
        tableHeight=247;
    }else{
        tableHeight=247+88;
    }
    CGRect tableViewFrame=CGRectMake(0, 120, UISCREEN_WIDTH, tableHeight);
    self.mytableView.frame=tableViewFrame;
    self.mytableView=[[UITableView alloc] initWithFrame:tableViewFrame];
    self.mytableView.separatorStyle=UITableViewCellEditingStyleNone;
    self.mytableView.delegate=self;
    self.mytableView.dataSource=self;
    //self.mytableView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:self.mytableView];
    self.arrayList=[NSMutableArray array];
    [self.arrayList addObject:@{@"headerView":@"memeber_add.png",@"list":@[@{@"name":@"ddfsdfsd"}]}];
      [self.arrayList addObject:@{@"headerView":@"memeber_vistied.png",@"list":@[
           @{@"name":@"ddfsdfsd"},
           @{@"name":@"ddfsdfsd"},
           @{@"name":@"ddfsdfsd"},
           @{@"name":@"ddfsdfsd"},
           @{@"name":@"ddfsdfsd"},
           @{@"name":@"ddfsdfsd"}
           ]}];

}
-(void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    AppDelegate *app=(AppDelegate*)[[UIApplication sharedApplication] delegate];
    self.hidesBottomBarWhenPushed=NO;
    [app.tabBarController hideExistingTabBar];
    app.tabBarController.tabBarView.hidden=NO;
    HomePageViewController *homePageViewController=(HomePageViewController*)[[[self.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0];
    homePageViewController.isShowLevelPop=YES;
    UIView *rightPopView=[self.view  viewWithTag:HOMEPAGELPV];
    [rightPopView removeFromSuperview];
}

-(void) onButtonClick:(UIButton*) sender{
    switch (sender.tag-MEMBERHOMEBUTTONTAG) {
        case 0:{//供稿数
            
            break;
        }
        case 1:{//评论数
            
            break;
        }
        case 2:{//收藏数
            
            break;
        }
    }
}


//表视图委托
#pragma mark - Tableview datasource & delegates
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIImageView *imageView=[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 29.0f)];
    NSString *imgepath=[[self.arrayList objectAtIndex:section] objectForKey:@"headerView"];
    imageView.image=[Utility imageNamed:imgepath];
    return imageView;
}

//指定有多少个分区(Section)，默认为1
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return [self.arrayList count];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section==0) {
        return 1;
    }
    return [[[self.arrayList objectAtIndex:section] objectForKey:@"list"] count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 29.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
    return cell.frame.size.height;
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentity = @"memberViewCell";
    static NSString *cellIdentity0 = @"memberViewCell0";
    MemberViewCell *cell = (MemberViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity];
    if (indexPath.section==0){
        cell = (MemberViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity0];
        if (cell ==  nil) {
            cell =(MemberViewCell*)[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity0];
         cell.frame=CGRectMake(0, 0, 320, 121);
         [cell addSubview:[self  cellOfSection0]];
        }
         return cell;
    }else {
        cell = (MemberViewCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentity];
        if (cell ==  nil) {
            cell = [[MemberViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentity];
             cell.frame=CGRectMake(0, 0, 320, 32);
        }
       
        cell.label1.text=[NSString stringWithFormat:@"%@  %@",@"2012-4-16",@"邀请"];
        cell.label2.text=@"怪兽";
        cell.label3.text=@"反嘴集团";
        cell.label4.text=@"新加入";
    }
   
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}

-(UIView *) cellOfSection0{
    UIView *sectionView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 121)];
    UILabel *label1=[Utility detailTextLabel:CGRectMake(12, 12, 150, 20) andFontSize:15.0f];
    label1.textColor=[UIColor blackColor];
    label1.text=[NSString stringWithFormat:@"饭局集团(%@人)",@"12"];
    [sectionView addSubview:label1];
    
    UILabel *label12=[Utility detailTextLabel:CGRectMake(220, 18, 90, 15) andFontSize:12.0f];
    label12.textColor=UIColorFromRGB(0x666666);
    label12.text=[NSString stringWithFormat:@"新增%@位新成员",@"12"];
     [sectionView addSubview:label12];
   UIScrollView *scrollView=[[UIScrollView alloc] initWithFrame:CGRectMake(12, 41, 296, 80)];
     scrollView.showsHorizontalScrollIndicator=NO;
    [scrollView setBackgroundColor:[UIColor  clearColor]];
    [sectionView addSubview:scrollView];
    
    
    NSArray *itemArray=@[@{@"pic":@"cg_logo.png",@"nickName":@"云吞面云吞面"}
                     ,@{@"pic":@"cg_logo.png",@"nickName":@"云吞面云吞面"}
                     ,@{@"pic":@"cg_logo.png",@"nickName":@"云吞面云吞面"}
                     ,@{@"pic":@"cg_logo.png",@"nickName":@"云吞面云吞面"}
                     ,@{@"pic":@"cg_logo.png",@"nickName":@"云吞面云吞面"}
                     ,@{@"pic":@"cg_logo.png",@"nickName":@"云吞面云吞面"}
                     ,@{@"pic":@"cg_logo.png",@"nickName":@"云吞面云吞面"}
                     ,@{@"pic":@"cg_logo.png",@"nickName":@"云吞面云吞面"}
                     ,@{@"pic":@"cg_logo.png",@"nickName":@"云吞面云吞面"}
                     ];
    float xPosition = 0;
    //第一按钮 默认显示
    for ( int i=0;i<itemArray.count;i++) {
        NSDictionary *dict=[itemArray objectAtIndex:i];
        CGRect frame=CGRectMake(xPosition, 12, 40, 40);
        UIButton *button=[Utility customButton:self action:@selector(thumbButtonAction:) normalImage:[NSString stringWithFormat:@"%@",[dict objectForKey:@"pic"]]lightedImage:[NSString stringWithFormat:@"%@",[dict objectForKey:@"pic"]] frame:frame title:nil tag:HOMEPAGESECONDCENTERBUTTONTAG+i];
        [scrollView addSubview:button];
        
        UILabel *label=[Utility detailTextLabel:CGRectMake( button.frame.origin.x-10, 53, button.frame.size.width+20, 15) andFontSize:12.0];
        label.text=[dict objectForKey:@"nickName"];
        label.textAlignment=NSTextAlignmentCenter;
        label.textColor=UIColorFromRGB(0x000000);
        [scrollView addSubview:label];
        
        
        xPosition += frame.size.width + 25;
    }
    [scrollView setContentSize:CGSizeMake(xPosition,scrollView.frame.size.height)];
    return sectionView;
}
-(void) thumbButtonAction:(UIButton*) sender{
    BowenMemberViewController *bowenMemberViewController=[[BowenMemberViewController alloc] initWithNibName:nil bundle:nil];
    [self.navigationController pushViewController:bowenMemberViewController animated:YES];
}

#pragma mark -searchBar delegate
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar{
    searchBar.showsScopeBar = YES;
    [searchBar sizeToFit];
    [searchBar setShowsCancelButton:YES animated:YES];
    return YES;
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar{
    [NSObject  cancelPreviousPerformRequestsWithTarget:self];
    
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar{
    searchBar.showsScopeBar = NO;
    [searchBar sizeToFit];
    [searchBar setShowsCancelButton:NO animated:YES];
    return YES;
}

-(void) onClickAction:(UIButton*) sender{
    HomePageViewController *homePageViewController=(HomePageViewController*)[[[self.tabBarController.viewControllers objectAtIndex:0] viewControllers] objectAtIndex:0];
    [homePageViewController  performSelector:@selector(onClickAction:) withObject:sender];
    
    
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    [searchBar  resignFirstResponder];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar  resignFirstResponder];
   
}




- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
